<template>
  <div class="collect-container">
    <div>
      <div class="bottomBar">
        
        <div class="right-container">
          <div class="title">收藏的商品</div>
        </div>
        <div class="left-container">
          <el-checkbox v-if="operaState"
            style="margin-right:30px;"
            :indeterminate="isIndeterminate"
            v-model="checkAll"
            @change="handleCheckAllChange">全选</el-checkbox>

          <el-link v-if="operaState"
          @click="operaCollectForDelete()"
            style="margin-right:30px;">取消收藏</el-link>
          <el-button size="mini"
            @click="opera()"
            type="warning">{{operaState ? '保存' : '批量管理'}}</el-button>
        </div>
      </div>
    </div>

    <div class="collect_container">
      <div class="goods_item"
        v-for="(item,index) in collect"
        :key="index">
        <img :src="item.commodity.picture" />
        <div class="goods_info">
          <p>{{item.commodity.name.slice(0,10)}}...</p>
          <p>￥{{item.commodity.price}}元</p>
        </div>

        <div class="bottom_btnGrp">
          <el-button  size="mini" @click="deleteCollectById(item.cid)">取消收藏</el-button>
          <el-button  size="mini">查看详情</el-button>
        </div>

        <el-checkbox-group v-model="checkedCities"
          v-if="operaState"
          @change="handleCheckedCitiesChange">
          <el-checkbox :label="item">
            <div class="tips"
              ref="tip"
              @click="demo(item,index)">
              <i class="el-icon-check"></i>
            </div>
          </el-checkbox>
        </el-checkbox-group>
      </div>
    </div>
  </div>
</template>

<script>
//api
import { getCollect, deleteCollect } from '@/utils/api/api.js';
export default {
  components: {},
  data() {
    return {
      user: {},
      //收藏
      collect: [],
      //当前标签页
      operaState: false,
      checkAll: false,
      checkedCities: [],
      cities: [],
      isIndeterminate: false,
    };
  },
  methods: {
    demo(item, index) {
      setTimeout(() => {
        if (this.checkedCities.length > 0) {
          this.checkedCities.forEach(object => {
            if (object.cid == item.cid) {
              this.$refs.tip[index].style = 'background-color:orange';
            } else {
              this.$refs.tip[index].style =
                'background-color:rgba(0, 0, 0, 0.3)';
            }
          });
        } else {
          this.$refs.tip[index].style = 'background-color:rgba(0, 0, 0, 0.3)';
        }
      }, 100);
    },

    handleCheckAllChange(val) {
      this.checkedCities = val ? this.collect : [];
      this.isIndeterminate = false;
      if (val) {
        this.$refs.tip.forEach(obj => {
          obj.style = 'background-color:orange';
        });
      } else {
        this.$refs.tip.forEach(obj => {
          obj.style = 'background-color:rgba(0, 0, 0, 0.3)';
        });
      }
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.collect.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.collect.length;
    },

    opera() {
      this.operaState = !this.operaState;
      this.checkedCities = [];
      this.isIndeterminate = false;

    },
    //获取收藏
    getCollectByUserId() {
      getCollect(this.user.uid).then(resp => {
        this.collect = resp.msg;
      });
    },
    //删除收藏
    deleteCollectById(idx) {
      deleteCollect(idx).then(resp => {
        if (resp.code == 200) {
          this.$message({
            type: 'success',
            message: '取消收藏成功',
          });
          this.getCollectByUserId();
        }
      });
    },
    //批量删除收藏
    operaCollectForDelete(){
      this.checkedCities.forEach(item=>{
        this.deleteCollectById(item.cid) 
      })
    }
  },
  created() {
    this.currentGoodsId = this.$route.query.goodsId;
    this.user = JSON.parse(localStorage.getItem('user'));
    this.getCollectByUserId();
  },
};
</script>

<style lang="scss" scoped>
.bottomBar {
  display: flex;
  width: 100%;
  margin: 10px auto;
  justify-content: space-between;
  align-items: center;
  background-color: #f8f8f8;
  .left-container {
    margin: 10px;
  }
  .right-container {
    display: flex;
    justify-content: left;
    align-items: center;
    .miniFont {
      font-size: 14px;
      color: red;
    }
    .normalFont {
      font-size: 30px;
      color: red;
    }
    .title {
      border-bottom: 2px solid orange;
      box-sizing: border-box;
      padding: 10px 6px;
      color: orange;
      margin-left: 20px;
    }
  }
}
.collect_container {
  width: 100%;
  box-sizing: border-box;
  padding: 20px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-flow: row wrap;
  .goods_item {
    width: 18%;
    margin: 8px 10px 8px 0;
    border: 1px solid #e6e6e6;
    align-items: center;
    text-align: center;
    position: relative;
    overflow: hidden;
    img {
      height: 130px;
    }
    .goods_info {
      margin-top: 10px;
      border-top: 1px solid #e6e6e6;
    }
    .goods_info > p:nth-child(1) {
      font-size: 12px;
      margin: 16px;
    }
    .goods_info > p:nth-child(2) {
      font-size: 15px;
      margin: 16px;
      color: rgb(255, 115, 0);
    }
    .bottom_btnGrp {
      display: flex;
      justify-content: space-between;
      .el-button:nth-child(1) {
        width: 50%;
        margin: 0;
        border-radius: 0;
        text-align: center;
        border-right: none;
        border-left: none;
        border-bottom: none
      }
      .el-button:nth-child(2) {
        width: 50%;
        margin: 0;
        border-radius: 0;
        text-align: center;
         border-bottom: none;
          border-right: none;
        
      }
    }
    .tips {
      position: absolute;
      height: 80px;
      width: 40px;
      top: -288px;
      right: -90px;
      background-color: rgba(0, 0, 0, 0.3);
      transform: rotate(-45deg);
      .el-icon-check {
        position: absolute;
        top: 29px;
        right: 16px;
        transform: rotate(45deg);
        font-size: 20px;
        font-weight: bolder;
        color: #ffffff;
      }
    }
  }
}

:deep(.el-tabs__content) {
  height: 100vh;
}
:deep(.el-tabs__item.is-active) {
  color: orange;
}
:deep(.el-tabs__active-bar) {
  background-color: orange;
}
:deep(.el-pagination.is-background .el-pager li:not(.disabled).active) {
  background-color: orange;
}
:deep(.el-checkbox-group .el-checkbox__input) {
  display: none;
}
:deep(.el-checkbox-group) {
  height: 0;
}
</style>